/*
 * Returns an new XMLHttpRequest object, or false if the browser doesn't support it
 */
function newXMLHttpRequest() {
 var xmlreq = false;

 //create XMLHttpRequest object in non-Microsoft browsers
 if (window.XMLHttpRequest) {
  xmlreq = new XMLHttpRequest();
 } else if (window.ActiveXObject) {

     try {
   //try to create XMLHttpRequest in later versions of IE
   xmlreq = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e1) {
   //failed to create required ActiveXObject
   try {
          //try version supported by older versions of IE
    xmlreq = new ActiveXObject("Microsoft.XMLHTTP");
   } catch (e2) {
          //unable to create an XMLHttpRequest by any means
    xmlreq = false;
   }
  }
 }

 return xmlreq;
}

/*
* returns a function that waits for the specified XMLHttpRequest
* to complete, then passes it XML response to the given handler function.
* req - The XMLHttpRequest whose state is changing
* responseXmlHandler - Function to pass the XML response to
*/
function getReadyStateHandler(req, responseXmlHandler) {
 //return an anonymous function that listens to the XMLHttpRequest instance
 return function () {
  //if the request's status is "complete"
  if (req.readyState == 4) {
   //check that we received a successful response from the server
   if (req.status == 200) {
    //pass the XML payload of the response to the handler function.
    responseXmlHandler(req.responseXML);
   } else {
    //an HTTP problem has occurred
    alert("HTTP error " + req.status + ": " + req.statusText);
   }
  }
 }
}

